<template>
  <div class="splash">
    <div class="content">
      <img src="@/assets/images/splash.jpg" alt="村宝易购" class="logo">
    </div>
  </div>
</template>

<script setup>
import { onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/store/modules/user'

const router = useRouter()
const userStore = useUserStore()

const checkLoginStatus = async () => {
  try {
    // 等待3秒
    await new Promise(resolve => setTimeout(resolve, 2500))
    
    // 检查是否有token
    const token = userStore.token
    if (!token) {
      router.replace('/login')
      return
    }

    // 验证token有效性并获取用户信息
    const isValid = await userStore.checkLoginStatus()
    if (!isValid) {
      router.replace('/login')
      return
    }

    // 根据用户类型跳转到不同页面
    if (userStore.isAdmin) {
      router.replace('/admin/home')
    } else {
      router.replace('/home')
    }
  } catch (error) {
    console.error('启动页面错误:', error)
    router.replace('/login')
  }
}

onMounted(() => {
  checkLoginStatus()
})
</script>

<style lang="scss" scoped>
.splash {
  width: 100%;
  height: 100%;
  .content {
    width: 100%;
    height: 100%;
    text-align: center;
    color: #fff;
    .logo {
      width: 100%;
      height: 100%;
      object-fit: cover;
    }
  }
}
</style> 